
tempfile bigresults


foreach xxx in 15_30 30_60 60_180 180_300 300_600 600_900 ///
				30_15 60_30 180_60 300_180 600_300 900_600{
	clear
	use "cleandata\winprob_matched_dataset`xxx'"
	
	
	keep if winprob!=. & timetaken!=. & timecontrol_iv!=. & size_match==2
	
	//bys matchvar: egen start_clock1=total(start_clock*(timecontrol_iv==0))
	
	//xtile turnbin=turn, n(10)
	
	
	bys matchvar: egen timetaken_1=total(timetaken*timecontrol_iv)
	bys matchvar: egen timetaken_0=total(timetaken*!timecontrol_iv)
	gen firststage=timetaken_1-timetaken_0 if size_match==2
		
	gen marginalvalue_firststage=marginalvalueoftime*firststage
	
	
	tempfile results
	forvalues t=1/5{		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==elo_percentile, absorb(matchvar)
		
		summ marginalvalue_firststage if `t'==elo_percentile
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & `t'==elo_percentile
		local meanfirststage=r(mean)
		
		summ moverelo if `t'==elo_percentile
		local meanelo=r(mean)
		
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen estimate_type="Marginal Benefit"
			gen start_clockbin=`t'
			gen meanelo=`meanelo'
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==elo_percentile, absorb(matchvar) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanelo=`meanelo'
			gen estimate_type="Marginal Cost"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
		
		gen diff_mb_mc=winprob-marginalvalue_firststage
		ivreghdfe diff_mb_mc (timetaken= timecontrol_iv) if `t'==elo_percentile, absorb(matchvar) cluster(matchvar)
		cap drop diff_mb_mc
		
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanelo=`meanelo'
			gen estimate_type="Difference"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
	}
	//preserve
	use `results', clear
	summ 
	
	if "`xxx'"=="30_60" | "`xxx'"=="30_15"{s
		local mytitle="30 seconds"
	}
	if "`xxx'"=="60_180" | "`xxx'"=="60_30"{
		local mytitle="1 minute"
	}
	if "`xxx'"=="180_300" | "`xxx'"=="180_60"{
		local mytitle="3 minutes"
	}
	if "`xxx'"=="300_180" | "`xxx'"=="300_600"{
		local mytitle="5 minutes"
	}
	if "`xxx'"=="600_300" | "`xxx'"=="600_900"{
		local mytitle="10 minutes"
	}
//	
// 	twoway rspike min95 max95 meanelo, color(forest_green) || ///
// 		scatter estimate meanelo, color(forest_green ) c(l) || ///
// 		line marginalvalueoftime meanelo, lcolor(dkorange) lpattern(dash) ///
// 		name(turns_`xxx', replace) nodraw title("`mytitle'") legend(order(2 3) label(2 "Marginal Benefit") label(3 "Marginal Cost")) ///
// 		xtitle("Elo") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))
	
	twoway rspike min95 max95 meanelo if estimate_type=="Marginal Benefit", color(forest_green) || ///
		scatter estimate meanelo if estimate_type=="Marginal Benefit", color(forest_green ) c(l) || /// 
		rspike min95 max95 meanelo if estimate_type=="Marginal Cost", color(dkorange) || ///
		scatter estimate meanelo if estimate_type=="Marginal Cost", color(dkorange ) c(l) lpattern(dash) ///
		name(elo_`xxx', replace) nodraw title("`mytitle'") legend(order(2 4) label(2 "Marginal Benefit") label(4 "Marginal Cost")) ///
		xtitle("Elo") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))
	
	//restore
	
	cap append using `bigresults'
	save `bigresults', replace
	
	
	
}
//
//
// graph combine turns_30_15 turns_60_30 turns_180_60 turns_300_180 turns_600_300 turns_900_600, name(lower_elo, replace)
// graph export "elo_comparison_lower.pdf", as(pdf) replace
//
// graph combine turns_15_30 turns_30_60 turns_60_180 turns_180_300 turns_300_600 turns_600_900, name(higher_elo, replace)
// graph export "elo_comparison_higher.pdf", as(pdf) replace






grc1leg  elo_30_60 elo_60_180 elo_180_300 elo_300_600 elo_600_900 elo_30_15 elo_60_30 elo_180_60 elo_300_180 elo_600_300, row(2) xcommon
graph export "exhibits/elo_comparison.pdf", as(pdf) replace



use `bigresults', clear


gen bonferoni=p*112/2 if (estimate<0 & inlist(time, "30_15", "60_30", "180_60", "300_180","600_300"))  | (estimate>0 & inlist(time,  "30_60", "60_180", "180_300","300_600", "600_900")) 


list time meanelo estimate p bonferoni if estimate_type=="Difference"
export delimited time meanelo estimate p bonferoni using "exhibits/elo_pvalues" if estimate_type=="Difference", replace